package top.hundreds.oasys.controller.FrontController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import top.hundreds.common.crud.bean.PageParam;
import top.hundreds.common.web.api.ApiResult;
import top.hundreds.oasys.bean.entity.Report;
import top.hundreds.oasys.bean.vo.ReportVo;
import top.hundreds.oasys.service.ReportService;

import javax.annotation.Resource;

/**
 * (Report)表控制层
 *
 * @author lcf
 * @since 2021-01-29 19:19:39
 */
@RestController
@RequestMapping("/front/report")
public class ReportController {
    /**
     * 服务对象
     */
    @Resource
    private ReportService reportService;

    /**
     * 查询单条汇报详情
     *
     * @param id 汇报id
     * @return 汇报实体类
     */
    @GetMapping("get")
    public ApiResult<ReportVo> selectOne(Long id) {
        Report report = this.reportService.get(id);
        ReportVo reportVo = this.reportService.toVo(report);
        return ApiResult.success(reportVo);
    }

    /**
     * 获取全部列表
     *
     * @param pageParam 分页参数
     * @return 汇报分页
     */
    @PostMapping("/list")
    public ApiResult<Page<ReportVo>> list(@RequestBody PageParam<Report> pageParam) {
        Page<Report> page = this.reportService.getPage(pageParam);
        Page<ReportVo> reportVoPage = this.reportService.toVoPage(page);
        return ApiResult.success(reportVoPage);
    }

    /**
     * 我的汇报
     *
     * @param pageParam 分页参数
     * @param userId    用户id
     * @return 汇报分页
     */
    @PostMapping("myReport")
    public ApiResult<Page<ReportVo>> myReport(@RequestBody PageParam<Report> pageParam, @RequestAttribute("id") Long userId) {
        Report andQuery = pageParam.getAndQuery();
        andQuery.setUserId(userId);
        pageParam.setAndQuery(andQuery);
        Page<Report> page = this.reportService.getPage(pageParam);
        Page<ReportVo> reportVoPage = this.reportService.toVoPage(page);
        return ApiResult.success(reportVoPage);
    }

    /**
     * 向我汇报的列表
     *
     * @param pageParam 分页参数
     * @param userId    用户id
     * @return 汇报分页
     */
    @PostMapping("reportForMe")
    public ApiResult<Page<ReportVo>> reportForMe(@RequestBody PageParam<Report> pageParam, @RequestAttribute("id") Long userId) {
        Report andQuery = pageParam.getAndQuery();
        andQuery.setToUserId(userId);
        pageParam.setAndQuery(andQuery);
        Page<Report> page = this.reportService.getPage(pageParam);
        Page<ReportVo> reportVoPage = this.reportService.toVoPage(page);
        return ApiResult.success(reportVoPage);
    }

    /**
     * 新增或更新
     *
     * @param report 汇报实体类
     * @return success
     */
    @PostMapping("save")
    public ApiResult<String> save(@RequestBody Report report) {
        if (report.getId() == null) {
            // 新增操作
            this.reportService.insert(report);
        } else {
            // 更新操作
            this.reportService.update(report);
        }
        return ApiResult.success();
    }

    /**
     * 删除汇报
     *
     * @param id 汇报id
     * @return success
     */
    @DeleteMapping("remove")
    public ApiResult<String> remove(Long id) {
        this.reportService.delete(id);
        return ApiResult.success();
    }

    /**
     * 接收汇报
     *
     * @param id 汇报id
     * @param userId 被汇报者id
     * @return success
     */
    @GetMapping("receive")
    public ApiResult<String> receive(Long id, @RequestAttribute("id") Long userId) {
        Report report = this.reportService.get(id);
        if (report.getToUserId().equals(userId)) {
            report.setStatus(1);
            return ApiResult.success();
        } else {
            return ApiResult.success("不可接收不是向你汇报的汇报");
        }
    }

}
